package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.LanguageHandlerModel;
import org.testis.gui.mvc.view.testdesign.LanguageHandlerView;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.testis.orm.classes.T_dmap_languages;
import org.testis.utils.middleware.LanguageDataHandler;


/**
 *
 * @author nurhat
 */
public class LanguageHandlerController extends AbstractController
{

    public static final String LANGUAGE_ADD_VISIBILITY =
        "AddVisibility";
    public static final String LANGUAGE_LOAD_VISIBILITY =
        "LoadVisibility";

    public static final String LANGUAGE_NAME = "LanguageName";
    public static final String LANGUAGE_ID = "LanguageId";
    public static final String DEVICE_ABSTRACTION_ID = "DeviceAbstractionId";
    public static final String DEVICE_ID = "DeviceId";
    public static final String FIRMWARE_ID = "FirmwareId";
    public static final String LANGUAGE_TEXT = "LanguageText";
    public static final String SHOW_VIEW = "LanguageViewVisibility";


    public LanguageHandlerController()
    {
        super();
        TestDesignControllerList.newInstance().setLanguageHandlerController(this);
    }


    public void startLoader()
    {
        initialize();
        setModelProperty(SHOW_VIEW,
                         true);
    }


    public String operationGetFileText()
    {
        return (String) getModelProperty(LANGUAGE_TEXT);
    }


    public void operationCloseWindow()
    {
        this.cleanAll();
    }


    public void initialize()
    {
        LanguageHandlerModel model = new LanguageHandlerModel();
        LanguageHandlerView view = new LanguageHandlerView(TestDesignControllerList.newInstance().
                                                           getParentFrame(),
                                                           true);
        super.addModel(model);
        super.addView(view);
        view.pack();
        operationChangeLoadVisibility(true);
    }


    public void operationChangeLanguageText(String val)
    {
        setModelProperty(LANGUAGE_TEXT,
                         val);
    }

    public void operationChangeAddVisibility(Boolean val)
    {
        setModelProperty(LANGUAGE_ADD_VISIBILITY,
                         val);
    }


    public void operationChangeLoadVisibility(Boolean val)
    {
        setModelProperty(LANGUAGE_LOAD_VISIBILITY,
                         val);
    }


    public void operationChangeFirmwareId(Integer val)
    {
        setModelProperty(FIRMWARE_ID,
                         val);
    }


    public void operationChangeDeviceId(Integer val)
    {
        setModelProperty(DEVICE_ID,
                         val);
    }


    public void operationChangeDeviceAbstractionId(Integer val)
    {
        setModelProperty(DEVICE_ABSTRACTION_ID,
                         val);
    }


    public void operationChangeLanguageName(String val)
    {
        setModelProperty(LANGUAGE_NAME,
                         val);
    }


    public HashMap operationGetLanguageMap()
    {
        LanguageDataHandler dh = new LanguageDataHandler();
        return dh.getAllLanguagesAsMap();
    }


    public HashMap operationGetLanguageMap(int deviceAbstractionId)
    {
        LanguageDataHandler dh = new LanguageDataHandler();
        return dh.getLanguagesByDeviceAbstractionIdAsMap(deviceAbstractionId);
    }


    public HashMap operationGetDeviceAbstractionMap()
    {
        LanguageDataHandler dh = new LanguageDataHandler();
        return dh.getAllDeviceAbstractionsAsMap();
    }


    public boolean operationDeleteLanguage(int langId)
    {
        LanguageDataHandler dh = new LanguageDataHandler();
        return dh.deleteLanguageById(langId);
    }


    public HashMap operationParseLanguagePairs(Integer langId,
                                               boolean printOut)
    {
        LanguageDataHandler dh = new LanguageDataHandler();
        HashMap hp = dh.getLanguagePairsAsMap(langId);
        if (printOut) {
            Iterator iter = hp.entrySet().
                iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                System.out.println(entry.getKey() + ":" +
                                   entry.getValue());
            }
        }
        return hp;
    }


    public void operationCancelSaveLanguage()
    {
        operationAddLanguage();
    }


    public void operationLoadLanguage(int langId)
    {
        LanguageDataHandler dh = new LanguageDataHandler();
        T_dmap_languages lang = dh.getLanguageById(langId);
        setModelProperty(LANGUAGE_ID,
                         langId);
        setModelProperty(LANGUAGE_NAME,
                         lang.getName());
        setModelProperty(DEVICE_ABSTRACTION_ID,
                         lang.getDeviceAbstractionId());
        setModelProperty(DEVICE_ID,
                         lang.getDeviceId());
        setModelProperty(FIRMWARE_ID,
                         lang.getFirmwareId());
        setModelProperty(LANGUAGE_TEXT,
                         lang.getFile());

    }


    public void operationAddLanguage()
    {
        setModelProperty(LANGUAGE_ID,
                         0);
        setModelProperty(LANGUAGE_NAME,
                         "");
        setModelProperty(DEVICE_ABSTRACTION_ID,
                         0);
        setModelProperty(DEVICE_ID,
                         0);
        setModelProperty(FIRMWARE_ID,
                         0);
        setModelProperty(LANGUAGE_TEXT,
                         "");
    }


    public void operationSaveLanguage(String langName,
                                      Integer deviceAbsId,
                                      String fileTxt)
    {
        if (fileTxt.equals("")) {
            fileTxt = (String) getModelProperty(LANGUAGE_TEXT);
        }
        LanguageDataHandler dh = new LanguageDataHandler();
        Integer langId =
            (Integer) getModelProperty(LANGUAGE_ID);
        dh.saveLanguage(langId,
                        langName,
                        deviceAbsId,
                        0,
                        0,
                        fileTxt);
        operationAddLanguage();
    }
}
